

/**
 * @function: do Rating.
 *   request parameters format:
 *      rating: score
 *      comment: the reason.
 *      rating=xx&comment=xxxxx
 *
 *   output parameter format:
 *     retStr
 *
 */

function doRate() {
    // 1. preparing parameters
    var $ratingForm = document.getElementById("rating-form");
    var rating ;
    // got rating value
    $ratingForm.rating.forEach(function(item,index) {
        if (item.checked) {
            rating = item.value ;
        }
    })
    var comment = document.getElementById("comment").value ;
    var paramStr = rating + "&" + comment ;
    // var paramStr = "rating=" + rating + "&comment=" + comment ;
    // 2. create XHR
    var xhr = createXMLHttpRequest() ;
    // 3. open
    xhr.open(
        "POST",
        "/01_jspBasics/test/rating"
    )
    // 4.callback
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {

                console.log("got response: " + xhr.responseText) ;
                // parsing response text: rating&comment
                var tokens = xhr.responseText.split("&") ;
                // render the response to related area (div. table, etc.).
                var $showMsg = document.getElementById("showMsg") ;
                $showMsg.innerHTML = "" ;
                tokens.forEach(function(item, index) {
                    $showMsg.innerHTML += index + " " + item + "<br>" ;
                })
                $showMsg.style.display = "" ;
            } else {
                alert("处理出错： "+ xhr.statusText) ;
            }

        }


    }
    // 5. send: only for POST request.
    xhr.send(paramStr) ;

}

